﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace Tetrinet
{
    public partial class SilverlightControl3 : UserControl, INotifyPropertyChanged
    {
        ObservableCollection<PlayingField> _playingFields;
        ObservableCollection<PlayingField> PlayingFields
        {
            get { return _playingFields; }
            set
            {
                if (_playingFields != value)
                {
                    _playingFields = value;
                    RisePropertyChanged("PlayingFields");
                }
            }
        }

        public void UpdateField(int[,] data)
        {
            for (int i = 0; i < PlayingFields.Count(); i++)
            {
                for (int j = 0; j < PlayingFields[i].Count(); j++)
                {
                    PlayingFields[i][j] = (byte)data[j, i];
                }
            }
        }

        public SilverlightControl3()
        {
            InitializeComponent();
            
            PlayingFields = new ObservableCollection<PlayingField>();
            for(int i =0; i<22; i++)
            {
                var temp = new PlayingField();
                temp.Add(new byte());
                temp.Add(new byte());
                temp.Add(new byte());
                temp.Add(new byte());
                temp.Add(new byte());
                temp.Add(new byte());
                temp.Add(new byte());
                temp.Add(new byte());
                temp.Add(new byte());
                temp.Add(new byte());
                temp.Add(new byte());
                temp.Add(new byte());
                PlayingFields.Add(temp);
            }
            
            listBox.ItemsSource = PlayingFields;
            
            Loaded += new RoutedEventHandler(SilverlightControl3_Loaded);
        }

        void SilverlightControl3_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void RisePropertyChanged(string name)
        {
            var temp = PropertyChanged;
            if (temp != null)
            {
                var arg = new PropertyChangedEventArgs(name);
                temp(this, arg);
            }
        }
    }

    public class PlayingField : ObservableCollection<byte>
    {
        
    }

    public class ColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string color = "Black";
            switch ((byte)value)
            {
                case 0:
                    color = "Black";
                    break;
                case 1:
                    color = "Blue";
                    break;
                case 2:
                    color = "Yellow";
                    break;
                case 3:
                    color = "Green";
                    break;
                case 4:
                    color = "Magneta";
                    break;
                case 5:
                    color = "Red";
                    break;
                case 6:
                    color = "Orange";
                    break;
                case 7:
                    color = "Orange";
                    break;
                case 8:
                    color = "Orange";
                    break;
                case 9:
                    color = "Orange";
                    break;

            }
            return color;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
